`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    08:29:14 04/30/2014 
// Design Name: 
// Module Name:    AudioCore 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module AudioCore(shoot, enemyShoot, explode, enemyExplode, music, clk, audioOUT, gain, shutdown_bar);


	 input clk; // main clock 100 MHz
	 input shoot, enemyShoot, explode, enemyExplode;	// signals to trigger a sound effect
	 input [2:0] music;  // selects the music for each level
			
			
	 output reg audioOUT;					// output signal to speaker
	 output gain,shutdown_bar;    // control signals for PMOD_AMP2
	 
	 assign gain = 1'b1; 			 // set gain to 6dB
	 assign shutdown_bar = 1'b1;   // set speaker to always enabled


////////////////////////////////////////////////////////////////	 
// CLK division  //  
///////////////////
	 wire  clk_6MHz,   // PWM carrier frequency
//		    clk_4Hz,    // musical beat frequency
			 clk_Mus,	 // musical beat frequency
			 clk_120Hz,  // sound effects frequency
			 clk_200Hz;  // sound effects frequency
			 
			 clk_div c6MHz(.clkIN(clk),
								.threshold(8), 
								.clkOUT(clk_6MHz));

			 clk_div cMus(.clkIN(clk),
							  .threshold(7500000), 
							  .clkOUT(clk_Mus));
							  
//			 clk_div c4Hz(.clkIN(clk),
//							  .threshold(12500000), 
//							  .clkOUT(clk_4Hz));

			 clk_div c120Hz(.clkIN(clk),
								 .threshold(416666), 
								 .clkOUT(clk_120Hz));
								 
			 clk_div c200Hz(.clkIN(clk),
								 .threshold(250000), 
								 .clkOUT(clk_200Hz));
////////////////////////////////////////////////////////////////	 



//------ sound effects ----------------------------------- 

	wire shoot1,shoot2,explode1,explode2, mus1;

	

	// when main character shoots
		shootSound sx1(.audioOUT(shoot1), 
							  .pwmCLK(clk_6MHz),
							  .soundCLK(clk_200Hz),
							  .enable(shoot));

	// when enemy shoots
	enemyShootSound sx2(.audioOUT(shoot2), 
							  .pwmCLK(clk_6MHz),
							  .soundCLK(clk_200Hz),
							  .enable(enemyShoot));


	// when main character is hit
		explodeSound ex1(.audioOUT(explode1), 
							  .pwmCLK(clk_6MHz),
							  .soundCLK(clk_120Hz),
							  .enable(explode));
	

	// when enemy is hit
  enemyExplodeSound ex2(.audioOUT(explode2), 
						 	   .pwmCLK(clk_6MHz),
							   .soundCLK(clk_120Hz),
							   .enable(enemyExplode));

	// level 1 music
				music1 mu1(.audioOUT(mus1), 
						 	   .pwmCLK(clk_6MHz),
							   .soundCLK(clk_Mus),
							   .enable(music[0]));


//--------------------------------------------------------


 
 always@(music,shoot,enemyShoot,explode,enemyExplode) begin
	
		if ((music[0]||music[1]||music[2]) &&
			 !(shoot||enemyShoot||explode||enemyExplode))
				begin
					audioOUT <= (mus1);
				end
		else begin
			case({shoot,enemyShoot,explode,enemyExplode})
				4'b 1000: audioOUT <= (shoot1);
				4'b 0100: audioOUT <= (shoot2);
				4'b 0010: audioOUT <= (explode1);
				4'b 0001: audioOUT <= (explode2);
				default: audioOUT <= 1'b0;
			endcase
		end
	end
 
endmodule

